package org.springblade.tms.module.exceptioncost.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.enums.OrderTypeEnum;
import org.springblade.tms.global.enums.WhetherEnum;
import org.springblade.tms.module.currencytype.entity.TMCurrencyType;
import org.springblade.tms.module.currencytype.service.ICurrencyTypeService;
import org.springblade.tms.module.demandorder.entity.TMDemandOrder;
import org.springblade.tms.module.demandorder.service.IDemandOrderService;
import org.springblade.tms.module.exceptioncost.entity.TMExceptionCost;
import org.springblade.tms.module.exceptioncost.service.IExceptionCostService;
import org.springblade.tms.module.exceptioncost.vo.TMExceptionCostVO;
import org.springblade.tms.module.exceptioncost.wrapper.ExceptionCostWrapper;
import org.springblade.tms.module.exceptioncosttype.entity.TMExceptionCostType;
import org.springblade.tms.module.exceptioncosttype.service.IExceptionCostTypeService;
import org.springblade.tms.module.exceptionreason.entity.TMExceptionReason;
import org.springblade.tms.module.exceptionreason.service.IExceptionReasonService;
import org.springblade.tms.module.exceptionrecord.entity.TMExceptionRecord;
import org.springblade.tms.module.exceptionrecord.service.IExceptionRecordService;
import org.springblade.tms.module.exceptiontype.entity.TMExceptionType;
import org.springblade.tms.module.exceptiontype.service.IExceptionTypeService;
import org.springblade.tms.module.sap.base.SapRequestService;
import org.springblade.tms.module.sap.dto.SapExceptionCostDTO;
import org.springblade.tms.module.sap.dto.SapExceptionCostDTOItem;
import org.springblade.tms.module.transportorder.entity.TMTransportOrder;
import org.springblade.tms.module.transportorder.service.ITransportOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.exceptioncost.base
 * @className: ExtraordinaryFeeService
 * @author: yr
 * @description: 异常费用服务
 * @date: 2022/4/24 13:57
 */
@Service
public class ExtraordinaryFeeService {
	@Resource
	private IExceptionCostService exceptionCostService;
	@Resource
	private IExceptionRecordService exceptionRecordService;
	@Resource
	private ICurrencyTypeService currencyTypeService;
	@Resource
	private ITransportOrderService transportOrderService;
	@Resource
	private IDemandOrderService demandOrderService;
	@Resource
	private IExceptionReasonService exceptionReasonService;
	@Resource
	private IExceptionTypeService exceptionTypeService;
	@Resource
	private IExceptionCostTypeService exceptionCostTypeService;
	@Resource
	private SapRequestService sapRequestService;
	/**
	 * @param id:
	 * @param query:
	 * @return R<IPage < TMExceptionCostVO>>
	 * @author yr
	 * @description 异常费用列表查询
	 * @date 2022/4/24 14:11
	 */
	public R<IPage<TMExceptionCostVO>> list(Long id, Query query) {
		IPage<TMExceptionCost> pages = exceptionCostService.page(Condition.getPage(query), Condition.getQueryWrapper(new TMExceptionCost())
			.lambda().eq(TMExceptionCost::getExceptionInfoId, id).orderByDesc(TMExceptionCost::getCreateTime,TMExceptionCost::getId));
		return R.data(ExceptionCostWrapper.build().pageVO(pages));
	}

	/**
	 * @param id:
	 * @return R
	 * @author yr
	 * @description 异常费用列表提交
	 * @date 2022/4/24 14:12
	 */
	public R submit(String id) {
		TMExceptionRecord tmExceptionRecord = exceptionRecordService.lambdaQuery().eq(TMExceptionRecord::getId, id).one();
		tmExceptionRecord.setAlreadyExceptionBill(WhetherEnum.yes.value());
		//上报sap
		SapExceptionCostDTO sapExceptionCostDTO = new SapExceptionCostDTO();
		if (tmExceptionRecord.getOrderType().equals(OrderTypeEnum.SINGLE_TRANSPORT.value())){
			TMTransportOrder tmTransportOrder = transportOrderService.getById(tmExceptionRecord.getParentId());
			sapExceptionCostDTO.setTorId(tmTransportOrder.getOrderNoNew());
			sapExceptionCostDTO.setTorType(tmTransportOrder.getOrderType());
		}else {
			TMDemandOrder tmDemandOrder = demandOrderService.getById(tmExceptionRecord.getParentId());
			sapExceptionCostDTO.setTorId(tmDemandOrder.getOrderNo());
			sapExceptionCostDTO.setTorType(tmDemandOrder.getOrderType());
		}
		TMExceptionReason tmExceptionReason = exceptionReasonService.getById(tmExceptionRecord.getExceptionTypeDesc());
		sapExceptionCostDTO.setEventReasonCode(tmExceptionReason.getReasonCode());
		sapExceptionCostDTO.setExceptionDate(tmExceptionRecord.getExceptionDate());
		sapExceptionCostDTO.setExceptionRemark(tmExceptionRecord.getRemark());
		TMExceptionType tmExceptionType = exceptionTypeService.getById(tmExceptionRecord.getExceptionType());
		sapExceptionCostDTO.setExceptionType(tmExceptionType.getEventReasonCode());
		List<TMExceptionCost> tmExceptionCostList = exceptionCostService.lambdaQuery().eq(TMExceptionCost::getExceptionInfoId, id).list();
		List<SapExceptionCostDTOItem> exceptionCostDTOItems = BeanUtil.copy(tmExceptionCostList, SapExceptionCostDTOItem.class);
		Map<String, List<TMExceptionCostType>> exceptionCostTypeMap = exceptionCostTypeService.list()
			.stream().collect(Collectors.groupingBy(TMExceptionCostType::getCostType));
		exceptionCostDTOItems.forEach(sapExceptionCostDTOItem -> sapExceptionCostDTOItem
			.setCostTypeDesc(exceptionCostTypeMap.get(sapExceptionCostDTOItem.getCostType()).get(0).getCostTypeDesc()));
		sapExceptionCostDTO.setSapExceptionCostDTOItems(exceptionCostDTOItems);
		sapRequestService.abnormalExpenses(sapExceptionCostDTO);
		return R.status(exceptionRecordService.updateById(tmExceptionRecord));
	}
}
